﻿// ===============================================================================
// XCI.WinForm.Platform 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using DevExpress.Images;
using DevExpress.Utils;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using XCI.Core;
using XCI.Sys.Model;

namespace XCI.Sys.Helper
{
    public class ModuleHelper
    {
        private readonly TreeList _tree;
        private readonly Dictionary<string, int> _moduleImageIndexs = new Dictionary<string, int>();

        public ModuleHelper(TreeList tree)
        {
            _tree = tree;
        }

        /// <summary>
        /// 展开节点
        /// </summary>
        public void ExpandNode()
        {
            ForEachDataSouce<SysModule>(p =>
             {
                 var node = _tree.FindNodeByKeyID(p.Id);
                 if (node == null)
                 {
                     return;
                 }
                 node.Expanded = p.IsExpand;
             });
        }

        public async Task InitAsync(ImageCollection imageList, bool isLargeImage = false)
        {
            await Task.Run(() =>
            {
                _moduleImageIndexs.Clear();
                imageList.Clear();
                imageList.BeginInit();
                imageList.Images.Add(isLargeImage ? R.DefaultModuleImage32 : R.DefaultModuleImage16);
                int countIndex = 1;
                ForEachDataSouce<SysModule>(p =>
                {
                    Image image = null;
                    if (p.IsMenu)
                    {
                        var imageName = p.WinCls;
                        if (!string.IsNullOrEmpty(imageName))
                        {
                            imageName = isLargeImage ? imageName.Replace("16x16", "32x32") : imageName.Replace("32x32", "16x16");
                            image = GetModuleImage(imageName) ?? GetModuleImage(p.WinCls);
                        }
                    }
                    else
                    {
                        image = isLargeImage ? R.DefaultPermissionImage32 : R.DefaultPermissionImage16;
                    }
                    if (image != null)
                    {
                        imageList.AddImage(image);
                        _moduleImageIndexs.Add(p.Id, countIndex);
                        countIndex++;
                    }
                });
                imageList.EndInit();
            }).ContinueWith(p =>
            {
                _tree.Refresh();
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }

        private Image GetModuleImage(string name)
        {
            return ImageResourceCache.Default.GetImage(name);
        }

        /// <summary>
        /// 获取模块图片序号
        /// </summary>
        /// <param name="node">节点</param>
        public int GetModuleImageIndex(TreeListNode node)
        {
            int index = 0;
            if (_moduleImageIndexs == null)
            {
                return index;
            }

            if (_tree.GetDataRecordByNode(node) is SysModule entity && _moduleImageIndexs.ContainsKey(entity.Id))
            {
                index = _moduleImageIndexs[entity.Id];
            }
            return index;
        }

        /// <summary>
        /// 循环数据源
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="action">执行动作</param>
        public void ForEachDataSouce<T>(Action<T> action) where T : new()
        {
            if (action == null) return;
            var source = _tree.DataSource;
            if (!(source is IList list)) return;
            foreach (T obj in list)
            {
                action(obj);
            }
        }
    }
}